//
//  MLAPropertyRepairVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/24.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAPropertyRepairVC.h"
#import "MLAPropertyRepairCell.h"
#import "MLAPropertyRepairEvaluteVC.h"
#import "MLAPropertyRepairApplyVC.h"
#import "MLAPropertyRepairModel.h"

@interface MLAPropertyRepairVC ()

@end

@implementation MLAPropertyRepairVC {
    NSMutableArray<MLAPropertyRepairModel *> *modelsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configTitle:@"报修"];
    [self configRightItemWithTitle:@"发起"];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[MLAPropertyRepairCell class] forCellReuseIdentifier:kRepairCellID];
    
    self.haveRefreshHeader = YES;
    [self loadData];
}

#pragma mark - LoadData
- (void)loadData {
    [super loadData];
    
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kRepairList params:nil success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self->modelsArr = [NSMutableArray array];
        self.page++;
        self.haveRefreshFooter = YES;
        self->modelsArr = [NSMutableArray array];
        self->modelsArr = [MLAPropertyRepairModel mj_objectArrayWithKeyValuesArray:response[@"lists"]];
        if (self->modelsArr.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
}

- (void)loadMoreData {
    NSDictionary *params = @{@"page":@(self.page)};
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kRunWaterList params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        
        self.page++;
        
        if ([response[@"lists"] count] < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        
        [self->modelsArr addObjectsFromArray:[MLAPropertyRepairModel mj_objectArrayWithKeyValuesArray:response[@"lists"]]];
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
}


#pragma mark - tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return modelsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MLAPropertyRepairCell *cell = [tableView dequeueReusableCellWithIdentifier:kRepairCellID forIndexPath:indexPath];
//    [cell.evaluteBtn addTarget:self action:@selector(evaluteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    MLAPropertyRepairModel *model = modelsArr[indexPath.row];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    MLAPropertyRepairModel *model = modelsArr[indexPath.row];
//    return model.dispose_type.integerValue == 1 ? 151: 205;
    return 151.f;
}


#pragma mark - Actions
//- (void)evaluteBtnAction:(UIButton *)sender {
//    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)sender.superview.superview.superview];
//    CXLog(@"评论 row=%ld",indexPath.row);
//    MLAPropertyRepairEvaluteVC *evaluteVC = [[MLAPropertyRepairEvaluteVC alloc] init];
//    [self.navigationController pushViewController:evaluteVC animated:YES];
//}

- (void)rightItemAction {
    CX_Log(@"发起报修申请");
    MLAPropertyRepairApplyVC *applyVC = [[MLAPropertyRepairApplyVC alloc] init];
    [self.navigationController pushViewController:applyVC animated:YES];
}

@end
